package com.hljx.config;

import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Map;


/**
 * @BelongsProject: hljx
 * @BelongsPackage: com.hljx.config
 * @Author: lp
 * @CreateTime: 2023-12-20  16:47
 * @Description: TODO
 * @Version: 1.0
 */

public class CustomInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (handler instanceof HandlerMethod) {

            HandlerMethod handlerMethod = (HandlerMethod) handler;
            CustomValidation loginIntercept =
                    handlerMethod.getMethod()//这一步是获取到我们要访问的方法
                            //然后根据我们制定的自定义注解的Class对象来获取到对应的注解
                            .getAnnotation(CustomValidation.class);


            if (null == loginIntercept) {
                System.out.println("访问的方法无需拦截");
                return true;
            } else {
                response.setContentType("application/json; charset=utf-8");
                // 获取请求的URL
                String requestUrl = request.getRequestURI();

                // 获取URL参数
                String jsonParam = ((MyRequestWrapper) request).getBody();

//            String parameterValue = request.getParameter("pageSize");
//            request.setAttribute("RequestBody", body);
                System.out.println(jsonParam);

            }
        }
        return true;


    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 在接口调用后进行处理，如果需要的话
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在接口调用完成后进行清理，如果需要的话
    }

    public static String getBody(ContentCachingRequestWrapper requestWrapper) {


        try (
                InputStream inputStream = requestWrapper.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
        ) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
